import { editInfo,addService,getDetail,deleteData,pay,configDetail,editConfig,reply } from '@/api/factory'
import { getToken, setToken, removeToken } from '@/utils/auth'

const getDefaultState = () => {
    return {
        token: getToken()
    }
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    SET_TOKEN: (state, token) => {
        state.token = token
    },
}

const actions = {
    editInfo({ commit },editData) {
        return new Promise((resolve, reject) => {
            editInfo(editData).then(response => {
                const { data } = response
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },
    addService({ commit },addData) {
        return new Promise((resolve, reject) => {
            addService(addData).then(response => {
                const { data } = response
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },
    getDetail({ commit },getData){
        return new Promise((resolve, reject) => {
            getDetail(getData).then(response => {
                const { data } = response
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    deleteData({ commit },delData){
        return new Promise((resolve, reject) => {
            deleteData(delData).then(response => {
                const { data } = response
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },
    pay({ commit },payData){
        return new Promise((resolve, reject) => {
            pay(payData).then(response => {
                const { data } = response
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    getConfig({ commit },data){
        return new Promise((resolve, reject) => {
            configDetail(data).then(response => {
                const { data } = response
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    editConfig({ commit },addData) {
        return new Promise((resolve, reject) => {
            editConfig(addData).then(response => {
                const { data } = response
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },
    reply({commit},replyData){
        return new Promise((resolve,reject)=>{
            reply(replyData).then(response=>{
                const {data} = response
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}

